package com.example.rk.service.impl;
import com.example.rk.common.LoggerSearchInfo;
import com.example.rk.common.MyPageInfo;
import com.example.rk.entity.Job;

import com.example.rk.mapper.JobMapper;
import com.example.rk.service.JobService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.options.LoggingOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper mapper;

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public String doAdd(Job job) {
        Job dbjob=mapper.findadd(job.getLocation(),job.getPosition());
        if (dbjob==null){
            mapper.create(job);
            return "ok";
        }else{
            return "exist";
        }

    }

    @Override
    public MyPageInfo findBySearch1(HttpServletRequest request, int id) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        int sid=id;
        String location = request.getParameter("location");
        String position = request.getParameter("position");
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        mapper.findBySearch1(sid,location,position);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(location);
        myPageInfo.setSearch2(position);
        myPageInfo.setSearch3(sid);
        return myPageInfo;

    }

    @Override
    public Job findById(int id) {
        return mapper.findByID(id);
    }

    @Override
    public String doEdit(Job job) {
        try {
            mapper.doEdit(job);
            return "ok";
        }catch (Exception e){
            System.out.println("RenKoServiceImpl.doEdit() ERROR:e="+e);
        }
        return "error";

    }

    @Override
    public List<Job> findonly(int id) {
        return mapper.findonly(id);
    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum=1;  //当前页
        int pageSize=5;  //每页大小
        String currentPageNumStr = request.getParameter("pageNum");
        if (currentPageNumStr!=null){
            currentPageNum=Integer.parseInt(currentPageNumStr);
        }
        String location = request.getParameter("location");
        String position = request.getParameter("position");
        LoggerSearchInfo.loggerAdminSearchInfo2("location",location);
        LoggerSearchInfo.loggerAdminSearchInfo3("position",position);
        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在执行sql之前执行
        mapper.findBySearch(location,position);
        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),
                pageInfo.getNavigateLastPage(),pageInfo.getTotal(),pageInfo.getList());
        myPageInfo.setSearch1(location);
        myPageInfo.setSearch2(position);
        return myPageInfo;

    }
}


